# -*- coding: utf-8 -*-
from tencentcloud.common import credential
from tencentcloud.common.exception.tencent_cloud_sdk_exception import (
    TencentCloudSDKException,
)

# 导入对应产品模块的client models。
from tencentcloud.sms.v20210111 import sms_client, models

# 导入可选配置类
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile

# from ApiManager import dev as DEV
# from iClouds.views import sems
from mysqldb.reconf import semsConf

# DEBUG = {}


# SecretId = 'AKID64pLcgUMzOXR4TdWBdqoX0ECxToulLjs'
# SecretKey = 'vpa9UbU9csumUyEG4e3HH3bu1vc8SbWD'
# SignName = "途云科技"
# SmsSdkAppId = "1400714639"
# TemplateId = "1492059"


# 实例化参数对象
sems = semsConf()


def Send_Sems_Sdk(obj):
    if not sems.status or not sems.is_active:
        raise serializers.ValidationError({"detail": "短信接口未启用"})
    try:
        # key对象
        cred = credential.Credential(sems.secretid, sems.secretkey)
        # requests对象
        httpProfile = HttpProfile()
        httpProfile.reqMethod = "POST"  # post请求(默认为post请求)
        httpProfile.reqTimeout = 30  # 请求超时时间，单位为秒(默认60秒)
        httpProfile.endpoint = "sms.tencentcloudapi.com"
        # header对象
        clientProfile = ClientProfile()
        clientProfile.signMethod = "TC3-HMAC-SHA256"  # 指定签名算法
        clientProfile.language = "en-US"
        clientProfile.httpProfile = httpProfile

        client = sms_client.SmsClient(cred, "ap-guangzhou", clientProfile)

        # post对象
        req = models.SendSmsRequest()
        req.SmsSdkAppId = sems.sessdkappid
        req.SignName = sems.signname
        req.TemplateId = sems.templateid
        req.SessionContext = ""
        req.ExtendCode = ""
        req.SenderId = ""
        req.PhoneNumberSet = [
            "+86" + obj.get("phone"),
        ]
        req.TemplateParamSet = [
            obj.get("code"),
            obj.get("quen"),
            str(obj.get("timeout")),
        ]

        resp = client.SendSms(req)

        return resp.to_json_string(indent=2)
    except BaseException as err:
        print(err)
        return False
